package reflectDemo;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86187
 * Date: 2022-08-23
 * Time: 22:45
 */

public class ReflectClassDemo {

    //通过反射创建对象
    public static void reflectNewInstance() {

        try {
            Class<?> c1 = Class.forName("reflectDemo.Student");
            Student student = (Student)c1.newInstance(); //newInstance() 创建类的实例

            System.out.println("通过反射创建的学生对象"+student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    // 反射私有的构造方法 屏蔽内容为获得公有的构造方法
    public static void reflectPrivateConstructor() {
        try {
            Class<?> c1 = Class.forName("reflectDemo.Student");
            Constructor<?> constructor =
                    c1.getDeclaredConstructor(String.class,int.class);

            constructor.setAccessible(true);//必不可少的一步 很重要

            System.out.println(constructor.newInstance("李兴伟", 22));
            Student student = (Student) constructor.newInstance("李兴伟1", 22);
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    //反射私有属性(字段)
    public static void reflectPrivateFiled() {
        try {
            Class<?> c1 = Class.forName("reflectDemo.Student");
            Student student = (Student)c1.newInstance();
            Field field =
                    c1.getDeclaredField("name");

            field.setAccessible(true);
            field.set(student,"美羊羊");
            System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    //反射 私有方法
    public static void reflectPrivateMethod() {
        try {
            Class<?> c1 = Class.forName("reflectDemo.Student");

            Student student = (Student) c1.newInstance();

            Method method = c1.getDeclaredMethod("function",String.class);
            method.setAccessible(true);

            method.invoke(student,"通过反射传参");

            //System.out.println(student);

        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (InstantiationException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (NoSuchMethodException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        //reflectNewInstance(); //通过反射创建对象 的函数

        //reflectPrivateConstructor(); // 反射私有的构造方法 屏蔽内容为获得公有的构造方法

        //reflectPrivateFiled();//反射私有属性(字段)

        reflectPrivateMethod();  //反射 私有方法
    }

}
